package com.woniuxy.shop.dao;

import java.util.ArrayList;
import java.util.List;

import org.junit.experimental.theories.FromDataPoints;

import com.woniuxy.shop.entity.DetailOrder;

/**
*@author 作者：
*version 创建时间：
*类说明：
*/
public class DetailOrderDao {
	
	/**
	 * 增加订单详细信息
	 * @param dor
	 */
	public void add(DetailOrder... dor) {
		String sql = "insert into shop_order_details(orderid,goodsid,nums,price) values(?,?,?,?)";
//		Object[] obs = {dor.getOrderid(),dor.getGoodsid(),dor.getNums(),dor.getPrice()};
//		DbHelper.excuteSQL(sql, obs);
		Object[][] params = new Object[dor.length][];
		for (int i = 0; i < params.length; i++) {
			params[i] = new Object[]{dor[i].getOrderid(),dor[i].getGoodsid(),dor[i].getNums(),dor[i].getPrice()};
		}
		DbHelper.executeBatchSQL(sql, params);
	}
	
	/**
	 * 修改指定订单的信息
	 * @param orderid
	 * @param dor
	 */
	public void update(int orderid,DetailOrder dor) {
		String sql = "update shop_order_details set goodsid=?,nums=?,price=? where orderid=?";
		Object[] obs = {dor.getGoodsid(),dor.getNums(),dor.getPrice(),dor.getOrderid()};
		DbHelper.excuteSQL(sql, obs);
	}
	
	
	/**
	 * 删除指定订单的信息
	 * @param orderid
	 */
	public void delete(int orderid) {
		String sql = "delete from shop_order_details where orderid=?";
		DbHelper.excuteSQL(sql,orderid);
	}
	
	/**
	 * 获取所有的详细订单
	 * @return
	 */
	public List<DetailOrder> getAllDetailOrder(int orderid){
		List<DetailOrder> deors = new ArrayList<>();
		String sql = "select od.*,g.name gname,g.image gimg from shop_order_details od,shop_goods g where od.orderid=? and od.goodsid=g.id";
		deors = DbHelper.executeQuery(sql, DetailOrder.class,orderid);
		return deors;
	}
	
	/**
	 * 查询指定订单的详细信息
	 * @param orderid
	 * @return
	 */
	public DetailOrder getDetailOrder(int orderid) {
		String sql = "select od.*,g.name,g.image from shop_order_details od,shop_goods g where od.orderid=? and od.goodsid=g.id";
		return DbHelper.executeQueryForSingleRow(sql, DetailOrder.class, orderid);
	}
}
